import request from '@/utils/request'

// 模拟菜单数据
const mockMenus = [
  {
    menuId: 1,
    menuName: '系统管理',
    parentId: 0,
    orderNum: 1,
    path: '/system',
    component: '',
    menuType: 'M',
    visible: '0',
    status: '0',
    perms: '',
    icon: 'Setting',
    createTime: '2024-03-27 10:00:00',
    children: [
      {
        menuId: 2,
        menuName: '用户管理',
        parentId: 1,
        orderNum: 1,
        path: 'user',
        component: 'system/user/index',
        menuType: 'C',
        visible: '0',
        status: '0',
        perms: 'system:user:list',
        icon: 'User',
        createTime: '2024-03-27 10:01:00'
      },
      {
        menuId: 3,
        menuName: '角色管理',
        parentId: 1,
        orderNum: 2,
        path: 'role',
        component: 'system/role/index',
        menuType: 'C',
        visible: '0',
        status: '0',
        perms: 'system:role:list',
        icon: 'UserFilled',
        createTime: '2024-03-27 10:02:00'
      },
      {
        menuId: 4,
        menuName: '菜单管理',
        parentId: 1,
        orderNum: 3,
        path: 'menu',
        component: 'system/menu/index',
        menuType: 'C',
        visible: '0',
        status: '0',
        perms: 'system:menu:list',
        icon: 'Menu',
        createTime: '2024-03-27 10:03:00'
      }
    ]
  },
  {
    menuId: 5,
    menuName: '车辆管理',
    parentId: 0,
    orderNum: 2,
    path: '/car',
    component: '',
    menuType: 'M',
    visible: '0',
    status: '0',
    perms: '',
    icon: 'Van',
    createTime: '2024-03-27 10:04:00',
    children: [
      {
        menuId: 6,
        menuName: '车辆列表',
        parentId: 5,
        orderNum: 1,
        path: 'list',
        component: 'car/list/index',
        menuType: 'C',
        visible: '0',
        status: '0',
        perms: 'car:list:list',
        icon: 'List',
        createTime: '2024-03-27 10:05:00'
      },
      {
        menuId: 7,
        menuName: '车主管理',
        parentId: 5,
        orderNum: 2,
        path: 'owner',
        component: 'car/owner/index',
        menuType: 'C',
        visible: '0',
        status: '0',
        perms: 'car:owner:list',
        icon: 'User',
        createTime: '2024-03-27 10:06:00'
      }
    ]
  }
]

// 查询菜单列表
export function listMenu(query) {
  // 模拟API调用
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        message: 'success',
        data: mockMenus
      })
    }, 500)
  })
}

// 查询菜单详细信息
export function getMenu(id) {
  // 模拟API调用
  return new Promise((resolve) => {
    setTimeout(() => {
      const findMenu = (menus, targetId) => {
        for (const menu of menus) {
          if (menu.menuId === parseInt(targetId)) {
            return menu
          }
          if (menu.children) {
            const found = findMenu(menu.children, targetId)
            if (found) return found
          }
        }
        return null
      }
      
      const menu = findMenu(mockMenus, id)
      resolve({
        code: 200,
        message: 'success',
        data: menu
      })
    }, 500)
  })
}

// 新增菜单
export function addMenu(data) {
  // 模拟API调用
  return new Promise((resolve) => {
    setTimeout(() => {
      const newMenu = {
        ...data,
        menuId: mockMenus.length + 1,
        createTime: new Date().toLocaleString()
      }
      
      if (data.parentId === 0) {
        mockMenus.push(newMenu)
      } else {
        const addToParent = (menus, parentId, newMenu) => {
          for (const menu of menus) {
            if (menu.menuId === parentId) {
              if (!menu.children) {
                menu.children = []
              }
              menu.children.push(newMenu)
              return true
            }
            if (menu.children) {
              if (addToParent(menu.children, parentId, newMenu)) {
                return true
              }
            }
          }
          return false
        }
        
        addToParent(mockMenus, data.parentId, newMenu)
      }
      
      resolve({
        code: 200,
        message: 'success',
        data: newMenu
      })
    }, 500)
  })
}

// 修改菜单
export function updateMenu(data) {
  // 模拟API调用
  return new Promise((resolve) => {
    setTimeout(() => {
      const updateInTree = (menus, targetId, newData) => {
        for (let i = 0; i < menus.length; i++) {
          if (menus[i].menuId === targetId) {
            menus[i] = { ...menus[i], ...newData }
            return true
          }
          if (menus[i].children) {
            if (updateInTree(menus[i].children, targetId, newData)) {
              return true
            }
          }
        }
        return false
      }
      
      updateInTree(mockMenus, data.menuId, data)
      resolve({
        code: 200,
        message: 'success',
        data: data
      })
    }, 500)
  })
}

// 删除菜单
export function deleteMenu(id) {
  // 模拟API调用
  return new Promise((resolve) => {
    setTimeout(() => {
      const deleteFromTree = (menus, targetId) => {
        for (let i = 0; i < menus.length; i++) {
          if (menus[i].menuId === parseInt(targetId)) {
            menus.splice(i, 1)
            return true
          }
          if (menus[i].children) {
            if (deleteFromTree(menus[i].children, targetId)) {
              return true
            }
          }
        }
        return false
      }
      
      deleteFromTree(mockMenus, id)
      resolve({
        code: 200,
        message: 'success',
        data: null
      })
    }, 500)
  })
}

// 批量删除菜单
export function batchDeleteMenu(ids) {
  // 模拟API调用
  return new Promise((resolve) => {
    setTimeout(() => {
      const idArray = ids.split(',').map(id => parseInt(id))
      idArray.forEach(id => {
        deleteMenu(id)
      })
      resolve({
        code: 200,
        message: 'success',
        data: null
      })
    }, 500)
  })
}

// 导出菜单数据
export function exportMenu(query) {
  // 模拟API调用
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        message: 'success',
        data: mockMenus
      })
    }, 500)
  })
}

// 获取菜单树形结构
export function getMenuTree() {
  // 模拟API调用
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        message: 'success',
        data: mockMenus
      })
    }, 500)
  })
}

// 获取角色菜单列表
export function getRoleMenus(roleId) {
  return request({
    url: '/getRoleMenus/' + roleId,
    method: 'get'
  })
}

// 更新角色菜单
export function updateRoleMenus(roleId, menuIds) {
  return request({
    url: '/updateRoleMenus/' + roleId,
    method: 'post',
    data: menuIds
  })
}

// 获取用户菜单列表
export function getUserMenus(userId) {
  return request({
    url: '/getUserMenus/' + userId,
    method: 'get'
  })
} 